package com.wangtao.firstapp.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;

import com.wangtao.firstapp.inter.OnRVItemClickListener;
import com.wangtao.firstapp.inter.OnRVItemLongClickListener;
import com.wangtao.firstapp.modles.CommunityHotRecommend;

import java.util.List;

/*
 * 晒单界面下热门推荐标签的自定义GridView的适配器
 * Created by lanouhn on 16/8/26.
 */
public class MyHotCommendAdapter extends RecyclerView.Adapter<MyHotCommendAdapter.ViewHolder> {
    private Context context;
    private List<CommunityHotRecommend.DataBean> recommends;
    private OnRVItemClickListener clickListener;
    private OnRVItemLongClickListener longClickListener;

    public void setClickListener(OnRVItemClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setLongClickListener(OnRVItemLongClickListener longClickListener) {
        this.longClickListener = longClickListener;
    }

    public MyHotCommendAdapter(Context context, List<CommunityHotRecommend.DataBean> recommends) {
        this.context = context;
        this.recommends = recommends;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_mygridview2, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        Picasso.with(context).load(recommends.get(position).getMini_pic_url()).config(Bitmap.Config.RGB_565).into(holder.iv_item_mygridview2);
        if (clickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int clickposition = holder.getLayoutPosition() - 1;
                    clickListener.OnRVItemClickListener(clickposition);
                }
            });
        }
        if (longClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int clickposition = holder.getLayoutPosition() - 1;
                    longClickListener.OnRVItemLongClickListener(clickposition);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return recommends.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_item_mygridview2;

        public ViewHolder(View itemView) {
            super(itemView);
            iv_item_mygridview2 = (ImageView) itemView.findViewById(R.id.iv_item_mygridview2);
        }
    }
}
